import { T_srv, Tsrv, ifs, ts, tvs, vue } from '@/services';
import { I_cmd, T_cmd } from './command';
import { N_cBaseChart } from './types';

/**
 * 默认图表配置
 */
export const DEFAULT_CHART_CONFIG: N_cBaseChart.ChartConfig = {
  chart: {
    id: 'default-chart',
    type: 'line',
    title: {
      text: '实时数据监控',
      left: 'center'
    },
    width: '100%',
    height: '400px',
    theme: 'default'
  },
  data: {
    source: {
      type: 'static',
      refreshInterval: 5000,
      maxPoints: 1000
    },
    mapping: {
      xAxis: 'time',
      series: [
        {
          name: '数据系列1',
          field: 'value',
          unit: '',
          color: '#5470c6'
        }
      ]
    },
    transform: {
      deduplication: {
        enabled: true,
        threshold: 0.001,
        strategy: 'average'
      }
    }
  },
  xAxis: {
    type: 'category',
    name: '时间',
    nameLocation: 'end'
  },
  yAxis: {
    type: 'value',
    name: '数值',
    nameLocation: 'end'
  },
  series: [
    {
      name: '数据系列1',
      type: 'line',
      smooth: true,
      symbol: 'emptyCircle',
      symbolSize: 4,
      animation: {
        enabled: true,
        duration: 300,
        easing: 'cubicOut'
      }
    }
  ],
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
      label: {
        backgroundColor: '#6a7985'
      }
    }
  },
  legend: {
    data: ['数据系列1'],
    right: 10,
    top: 10
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  }
};

/**
 * 配置验证函数
 */
export function validateChartConfig(config: N_cBaseChart.ChartConfig): boolean {
  if (!config.chart || !config.chart.id || !config.chart.type) {
    console.error('图表配置缺少必要字段: chart.id 或 chart.type');
    return false;
  }

  if (!config.data || !config.data.source || !config.data.mapping) {
    console.error('数据配置缺少必要字段');
    return false;
  }

  if (config.data.source.type === 'scada-api' && !config.data.source.endpoint) {
    console.error('SCADA API类型需要配置endpoint');
    return false;
  }

  if (!config.xAxis || !config.yAxis) {
    console.error('坐标轴配置缺少必要字段');
    return false;
  }

  if (!config.series || config.series.length === 0) {
    console.error('系列配置不能为空');
    return false;
  }

  return true;
}

/**
 * 配置合并函数
 */
export function mergeChartConfig(
  baseConfig: N_cBaseChart.ChartConfig,
  overrideConfig: Partial<N_cBaseChart.ChartConfig>
): N_cBaseChart.ChartConfig {
  return {
    ...baseConfig,
    ...overrideConfig,
    chart: {
      ...baseConfig.chart,
      ...overrideConfig.chart
    },
    data: {
      ...baseConfig.data,
      ...overrideConfig.data,
      source: {
        ...baseConfig.data.source,
        ...overrideConfig.data?.source
      },
      mapping: {
        ...baseConfig.data.mapping,
        ...overrideConfig.data?.mapping,
        series: [
          ...(baseConfig.data.mapping.series || []),
          ...(overrideConfig.data?.mapping?.series || [])
        ]
      },
      transform: {
        ...baseConfig.data.transform,
        ...overrideConfig.data?.transform
      }
    },
    xAxis: {
      ...baseConfig.xAxis,
      ...overrideConfig.xAxis
    },
    yAxis: {
      ...baseConfig.yAxis,
      ...overrideConfig.yAxis
    },
    series: [
      ...(baseConfig.series || []),
      ...(overrideConfig.series || [])
    ]
  };
}

/**
 * 获取有效的图表配置
 */
function getValidChartConfig(config: Partial<N_cBaseChart.ChartConfig>): N_cBaseChart.ChartConfig {
  const baseConfig = DEFAULT_CHART_CONFIG;
  
  if (!config || Object.keys(config).length === 0) {
    return baseConfig;
  }

  const mergedConfig = mergeChartConfig(baseConfig, config);
  
  if (!validateChartConfig(mergedConfig)) {
    console.warn('配置验证失败，使用默认配置');
    return baseConfig;
  }

  return mergedConfig;
}

export { I_cmd, N_cBaseChart, T_cmd, T_srv, Tsrv, getValidChartConfig, ifs, ts, tvs, vue };
